<%@ page language="java" contentType="text/html; charset=UTF8" pageEncoding="UTF8"%>
<%@ page import="java.util.*" %>
<%@ page import="com.bun.ipmon.service.IpMonService" %>
<%@ page import="com.google.appengine.api.memcache.MemcacheService" %>
<%@ page import="com.google.appengine.api.memcache.MemcacheServiceFactory" %>
<%@ page import="com.google.appengine.api.memcache.ErrorHandlers" %>
<%@ page import="com.google.appengine.api.memcache.Expiration" %>
<%@ page import="java.util.logging.Level" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%
	IpMonService ipMonService = new IpMonService();
	boolean hasPhone = ipMonService.hasPhone();
	MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
	syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
	
	if(hasPhone){
		out.println("has phone<br/><br/><div style='white-space: nowrap; overflow: hidden;'>");
		String mailSent = (String) syncCache.get("mailSent");
		if(mailSent == null){
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.HOUR, +8);
			Date todayDate = cal.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			ipMonService.sendMail("Has phone "+sdf.format(todayDate), (String) syncCache.get("detail"));
			syncCache.put("mailSent",sdf.format(todayDate),Expiration.byDeltaSeconds(300));
		}
		
	}
	else{
		out.println("no phone<br/><br/><div style='white-space: nowrap; overflow: hidden;'>");
	}
	out.println(syncCache.get("detail"));
	out.println("</div>");
%>
